package com.lishu.carsafe.http;

import com.alibaba.fastjson.JSONObject;
import com.lishu.carsafe.constant.UserPreferences;
import com.lishu.carsafe.model.BaseModel;
import com.lishu.carsafe.model.ContactDetailModel;
import com.lishu.carsafe.model.ContactListModel;
import com.lishu.carsafe.model.FaultDetailModel;
import com.lishu.carsafe.model.FaultListModel;
import com.lishu.carsafe.model.FaultToAddModel;
import com.lishu.carsafe.model.MonitorActiveInfoModel;
import com.lishu.carsafe.model.MonitorBaseInfoModel;
import com.lishu.carsafe.model.MonitorListModel;
import com.lishu.carsafe.model.ScheduleDetailModel;
import com.lishu.carsafe.model.ScheduleListModel;
import com.lishu.carsafe.model.ScheduleSignInfoModel;
import com.lishu.carsafe.model.StatisticDetailModel;
import com.lishu.carsafe.model.TaskDetailModel;
import com.lishu.carsafe.model.TaskListModel;
import com.lishu.carsafe.model.UploadFileModel;
import com.lishu.carsafe.model.UserModel;
import com.lishu.carsafe.model.VersionModel;
import com.lishu.carsafe.model.entity.Creator;
import com.lishu.carsafe.model.entity.Level;
import com.lishu.carsafe.model.entity.Type;
import com.lishu.carsafe.model.entity.VehicleId;
import com.lishu.carsafe.utils.MD5;

import org.xutils.common.util.KeyValue;
import org.xutils.http.RequestParams;
import org.xutils.http.body.MultipartBody;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * http接口
 */
public class HttpLoader extends HttpBase {

    //---------------------------------- 3.1 用户接口 -----------------------------------------------
    //3.1.1	登录接口
    public static void login(String username, String password, IResponseListener listener) {
        RequestParams params = new RequestParams(HttpAddress.LOGIN);
        params.addBodyParameter("loginName", username);
        params.addBodyParameter("password", password);
        //params.addBodyParameter("password", new MD5().getMd5(password));

        doLoginPost(getParams(params), UserModel.class, listener);
    }

    //---------------------------------- 3.2 我的排班接口 ---------------------------------------------
    //3.2.1	获取排班列表
    public static void getScheduleList(String rangeTime, IResponseListener listener) {
        RequestParams params = new RequestParams(HttpAddress.GET_SCHEDULE_LIST);
        params.addBodyParameter("usercode", UserPreferences.getInstance().getUserId());
        params.addBodyParameter("rangeTime", rangeTime);

        doPost(getParams(params), ScheduleListModel.class, listener);
    }
    //3.2.2	获取线路详情
    public static void getScheduleLineDetail(String lineId, IResponseListener listener) {
        RequestParams params = new RequestParams(HttpAddress.GET_SCHEDULE_LINE_DETAIL);
        params.addBodyParameter("lineId", lineId);

        doGet(getParams(params), ScheduleDetailModel.class, listener);
    }
    //3.2.4 排班签到
    public static void scheduleSignIn(String id, String signAddress, String signTime, String signImageUrl, IResponseListener listener) {
        JSONObject request = new JSONObject();
        request.put("id", id);
        request.put("signAddress", signAddress);
        request.put("signTime", signTime);
        request.put("signImageUrl", signImageUrl);
        RequestParams params = getJsonParams(HttpAddress.SCHEDULE_SIGN_IN, request);

        doPost(params, BaseModel.class, listener);
    }
    //3.2.3	排班请假
    public static void scheduleLeave(String id, String remark, IResponseListener listener) {
        JSONObject request = new JSONObject();
        request.put("id", id);
        request.put("remark", remark);
        RequestParams params = getJsonParams(HttpAddress.SCHEDULE_LEAVE, request);

        doPost(params, BaseModel.class, listener);
    }
    //3.2.5	取消排班请假
    public static void scheduleCancelLeave(String id, IResponseListener listener) {
        RequestParams params = new RequestParams(HttpAddress.SCHEDULE_CANCEL_LEAVE);
        params.addBodyParameter("id", id);

        doPost(getParams(params), BaseModel.class, listener);
    }
    //3.2.6	获取签到信息
    public static void scheduleSignInfo(String id, IResponseListener listener) {
        RequestParams params = new RequestParams(HttpAddress.SCHEDULE_SIGN_INFO);
        params.addBodyParameter("id", id);

        doPost(getParams(params), ScheduleSignInfoModel.class, listener);
    }
    //3.2.7 上传签到照片 (同3.3.4)

    //---------------------------------- 3.3 任务单接口 ----------------------------------------------
    //3.3.1	获取任务单列表
    public static void getTaskList(String startTime, String endTime, String pageNumber,
                                   String pageSize, IResponseListener listener) {
        RequestParams params = new RequestParams(HttpAddress.GET_TASK_LIST);
        params.addBodyParameter("usercode", UserPreferences.getInstance().getUserId());
        params.addBodyParameter("startTime", startTime);
        params.addBodyParameter("endTime", endTime);
        params.addBodyParameter("pageNumber", pageNumber);
        params.addBodyParameter("pageSize", pageSize);

        doPost(getParams(params), TaskListModel.class, listener);
    }
    //3.3.2 获取任务单详细内容
    public static void getTaskDetail(String taskId, IResponseListener listener) {
        RequestParams params = new RequestParams(HttpAddress.GET_TASK_DETAIL);
        params.addBodyParameter("taskId", taskId);

        doGet(getParams(params), TaskDetailModel.class, listener);
    }
    //3.3.3	处理任务
    public static void taskHandle(String taskId, String content, String handleTime,
                                   String mediaType, String mediaUrl, IResponseListener listener) {
        RequestParams params = new RequestParams(HttpAddress.TASK_HANDLE);
        params.addBodyParameter("taskId", taskId);
        params.addBodyParameter("content", content);
        params.addBodyParameter("handleTime", handleTime);
        params.addBodyParameter("mediaType", mediaType);
        params.addBodyParameter("mediaUrl", mediaUrl);

        doPost(getParams(params), BaseModel.class, listener);
    }
    //3.3.4 上传处理多媒体
    public static void uploadFile(String type, String filePath, IResponseListener listener) {
        RequestParams params = new RequestParams(HttpAddress.FILE_UPLOAD);
        params.addBodyParameter("filePackage", type);
        //params.addBodyParameter("file", new File(filePath));
        List<KeyValue> list = new ArrayList<>();
        File imageFile = new File(filePath);
        if (imageFile.exists()) {
            list.add(new KeyValue("file", imageFile));
        }
        MultipartBody body = new MultipartBody(list, "UTF-8");
        params.setRequestBody(body);

        doPost(params, UploadFileModel.class, listener);
    }

    //---------------------------------- 3.4 故障上报 -----------------------------------------------
    //3.4.1	获取故障列表
    public static void getFaultList(String startTime, String endTime, String pageNumber,
                                   String pageSize, IResponseListener listener) {
        RequestParams params = new RequestParams(HttpAddress.GET_FAULT_LIST);
        params.addBodyParameter("usercode", UserPreferences.getInstance().getUserId());
        params.addBodyParameter("startTime", startTime);
        params.addBodyParameter("endTime", endTime);
        params.addBodyParameter("pageNumber", pageNumber);
        params.addBodyParameter("pageSize", pageSize);

        doPost(getParams(params), FaultListModel.class, listener);
    }
    //3.4.2	获取故障详情
    public static void getFaultDetail(String faultId, IResponseListener listener) {
        RequestParams params = new RequestParams(HttpAddress.GET_FAULT_DETAIL);
        params.addBodyParameter("faultId", faultId);

        doGet(getParams(params), FaultDetailModel.class, listener);
    }
    //3.4.3	修改故障
    public static void faultUpdate(String id, String title, VehicleId vehicleId,
                                   String addres, Level level, Type type,
                                   String content, String createTime, String mediaType,
                                   String mediaUrl, IResponseListener listener) {
        JSONObject request = new JSONObject();
        request.put("id", id);
        request.put("title", title);
        request.put("vehicleId", vehicleId);
        request.put("addres", addres);
        request.put("level", level);
        request.put("type", type);
        request.put("content", content);
        request.put("createTime", createTime);
        request.put("mediaType", mediaType);
        request.put("mediaUrl", mediaUrl);
        RequestParams params = getJsonParams(HttpAddress.FAULT_UPDATE, request);

        doPost(params, BaseModel.class, listener);
    }
    //3.4.4	获取新增故障的可选项
    public static void getFaultToAdd(IResponseListener listener) {
        RequestParams params = new RequestParams(HttpAddress.FAULT_TO_ADD);

        doGet(getParams(params), FaultToAddModel.class, listener);
    }
    //3.4.5	新增故障
    public static void faultAdd(String title, VehicleId vehicleId, String addres,
                                Level level, Type type, String content,
                                Creator creator, String createTime, String mediaType,
                                String mediaUrl, IResponseListener listener) {
        JSONObject request = new JSONObject();
        request.put("title", title);
        request.put("vehicleId", vehicleId);
        request.put("addres", addres);
        request.put("level", level);
        request.put("type", type);
        request.put("content", content);
        request.put("creator", creator);
        request.put("createTime", createTime);
        request.put("mediaType", mediaType);
        request.put("mediaUrl", mediaUrl);
        RequestParams params = getJsonParams(HttpAddress.FAULT_ADD, request);

        doPost(params, BaseModel.class, listener);
    }
    //3.4.6 撤销故障
    public static void getFaultRepeal(String faultId, IResponseListener listener) {
        RequestParams params = new RequestParams(HttpAddress.FAULT_REPEAL);
        params.addBodyParameter("faultId", faultId);

        doPost(getParams(params), BaseModel.class, listener);
    }
    //3.4.7 上传故障多媒体 (同3.3.4)

    //---------------------------------- 3.5 通讯录 -------------------------------------------------
    //3.5.1	获取通讯录列表
    public static void getContactList(IResponseListener listener) {
        RequestParams params = new RequestParams(HttpAddress.GET_CONTACT_LIST);
        params.addBodyParameter("usercode", UserPreferences.getInstance().getUserId());

        doPost(getParams(params), ContactListModel.class, listener);
    }
    //3.5.2 获取联系人详情
    public static void getContactDetail(String contactId, IResponseListener listener) {
        RequestParams params = new RequestParams(HttpAddress.CONTACT_DETAIL);
        params.addBodyParameter("contactId", contactId);

        doGet(getParams(params), ContactDetailModel.class, listener);
    }

    //---------------------------------- 3.6 运营监测 ------------------------------------------------
    //3.6.1	获取运营监测列表
    public static void getMonitorList(IResponseListener listener) {
        RequestParams params = new RequestParams(HttpAddress.GET_MONITOR_LIST);
        params.addBodyParameter("usercode", UserPreferences.getInstance().getUserId());

        doPost(getParams(params), MonitorListModel.class, listener);
    }
    //3.6.2	实时监测-基础的车辆和线路数据
    public static void getMonitorBaseInfo(String vehicleId, String lineId, IResponseListener listener) {
        RequestParams params = new RequestParams(HttpAddress.GET_MONITOR_BASE_INFO);
        params.addBodyParameter("vehicleId", vehicleId);
        params.addBodyParameter("lineId", lineId);

        doPost(getParams(params), MonitorBaseInfoModel.class, listener);
    }
    //3.6.3 实时监测-动态的车辆、设备、坐标等信息
    public static void getMonitorActiveInfo(String vehicleId, String lineId, IResponseListener listener) {
        RequestParams params = new RequestParams(HttpAddress.GET_MONITOR_ACTIVE_INFO);
        params.addBodyParameter("vehicleId", vehicleId);
        params.addBodyParameter("lineId", lineId);

        doPost(getParams(params), MonitorActiveInfoModel.class, listener);
    }

    //---------------------------------- 3.7 统计分析 -----------------------------------------------
    //3.7.1 获取统计分析数据
    public static void getStatisticDetail(String vehicleId, String type, String range, IResponseListener listener) {
        RequestParams params = new RequestParams(HttpAddress.GET_STATISTIC_DETAIL);
        params.addBodyParameter("usercode", UserPreferences.getInstance().getUserId());
        params.addBodyParameter("vehicleId", vehicleId);
        params.addBodyParameter("type", type);
        params.addBodyParameter("range", range);

        doPost(getParams(params), StatisticDetailModel.class, listener);
    }

    //---------------------------------- 3.8 App版本更新 --------------------------------------------
    //3.8.1 查询最新版本
    public static void latestVersion(String versionCode, IResponseListener listener) {
        RequestParams params = new RequestParams(HttpAddress.LATEST_VERSION);
        params.addBodyParameter("softCode", "safetyOfficer_app");
        params.addBodyParameter("versionCode", versionCode);

        doPost(getParams(params), VersionModel.class, listener);
    }
    //3.8.2	上传新的版本APP文件 (同3.3.4)
    //3.8.3	软件新增版本
    public static void addVersion(String versionNo, String versionCode, String downloadUrl,
                                  String softwareId, IResponseListener listener) {
        RequestParams params = new RequestParams(HttpAddress.ADD_VERSION);
        params.addBodyParameter("versionNo", versionNo);
        params.addBodyParameter("versionCode", versionCode);
        params.addBodyParameter("downloadUrl", downloadUrl);
        params.addBodyParameter("softwareId", softwareId);

        doPost(getParams(params), BaseModel.class, listener);
    }
}
